package com.huawei.hms.push;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Parcelable;
import android.text.TextUtils;
import com.huawei.hms.android.HwBuildEx.VERSION;
import com.huawei.hms.api.Api;
import com.huawei.hms.api.Api$ApiOptions$NoOptions;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.common.HuaweiApi;
import com.huawei.hms.common.internal.Preconditions;
import com.huawei.hms.core.aidl.IMessageEntity;
import com.huawei.hms.push.c.a;
import com.huawei.hms.push.c.c;
import com.huawei.hms.support.api.a.a.b;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.JsonUtil;
import com.huawei.hms.utils.NetWorkUtil;
import java.util.regex.Pattern;
import o.fjo;
import o.fjw;
import o.fjx;

public class HmsMessaging {
    private static final Pattern a = Pattern.compile("[\\u4e00-\\u9fa5a-zA-Z0-9-_.~%]{1,900}");
    private static HmsMessaging b;
    private Context c;
    private HuaweiApi<Api$ApiOptions$NoOptions> d;

    private HmsMessaging(Context context) {
        Preconditions.checkNotNull(context);
        this.c = context.getApplicationContext();
        Api api = new Api(HuaweiApiAvailability.HMS_API_NAME_PUSH);
        if (context instanceof Activity) {
            this.d = new HuaweiApi((Activity) context, api, null, new c());
        } else {
            this.d = new HuaweiApi(context, api, null, new c());
        }
        this.d.setKitSdkVersion(30003301);
    }

    public static synchronized HmsMessaging getInstance(Context context) {
        HmsMessaging hmsMessaging;
        synchronized (HmsMessaging.class) {
            if (b == null) {
                b = new HmsMessaging(context);
            }
            hmsMessaging = b;
        }
        return hmsMessaging;
    }

    public boolean isAutoInitEnabled() {
        return false;
    }

    public void setAutoInitEnabled(boolean z) {
    }

    public fjo<Void> subscribe(String str) {
        String a = a.a(this.c, "push.subscribe");
        HMSLog.i("HmsMessaging", "invoke subscribe");
        return a(str, "Sub", a);
    }

    public fjo<Void> unsubscribe(String str) {
        String a = a.a(this.c, "push.subscribe");
        HMSLog.i("HmsMessaging", "invoke unsubscribe");
        return a(str, "UnSub", a);
    }

    private fjo<Void> a(String str, String str2, String str3) {
        if (str == null || !a.matcher(str).matches()) {
            a.a(this.c, "push.subscribe", str3, com.huawei.hms.push.a.a.ERROR_ARGUMENTS_INVALID);
            throw new IllegalArgumentException("Invalid topic name: topic does not match the allowed format:[\\u4e00-\\u9fa5a-zA-Z0-9-_.~%]{1,900}");
        }
        try {
            if (!com.huawei.hms.support.api.push.c.a.a()) {
                throw com.huawei.hms.push.a.a.a(com.huawei.hms.push.a.a.ERROR_OPERATION_NOT_SUPPORTED);
            } else if (NetWorkUtil.getNetworkType(this.c) == 0) {
                throw com.huawei.hms.push.a.a.a(com.huawei.hms.push.a.a.ERROR_NO_NETWORK);
            } else {
                return this.d.doWrite(new com.huawei.hms.push.b.a("push.subscribe", JsonUtil.createJsonString(new b(this.c.getPackageName(), str2, str)), str3));
            }
        } catch (Exception e) {
            fjx o_fjx = new fjx();
            o_fjx.ॱ(e);
            a.a(this.c, "push.subscribe", str3, e.getStatusCode());
            return o_fjx.ॱ();
        } catch (Exception e2) {
            fjx o_fjx2 = new fjx();
            o_fjx2.ॱ(com.huawei.hms.push.a.a.a(com.huawei.hms.push.a.a.ERROR_INTERNAL_ERROR));
            a.a(this.c, "push.subscribe", str3, com.huawei.hms.push.a.a.ERROR_INTERNAL_ERROR);
            return o_fjx2.ॱ();
        }
    }

    public void send(RemoteMessage remoteMessage) {
        String a = a.a(this.c, "push.sendMessage");
        HMSLog.i("HmsMessaging", "send upstream message");
        a(remoteMessage, a);
    }

    private void a(RemoteMessage remoteMessage, String str) {
        if (!com.huawei.hms.support.api.push.c.a.a()) {
            HMSLog.e("HmsMessaging", "operation available only on Huawei device with EMUI 10.0 or higher");
            a.a(this.c, "push.sendMessage", str, com.huawei.hms.push.a.a.ERROR_OPERATION_NOT_SUPPORTED);
            throw new UnsupportedOperationException("operation available only on Huawei device with EMUI 10.0 or higher");
        } else if (TextUtils.isEmpty(remoteMessage.getTo())) {
            HMSLog.e("HmsMessaging", "Missing 'to'");
            a.a(this.c, "push.sendMessage", str, com.huawei.hms.push.a.a.ERROR_ARGUMENTS_INVALID);
            throw new IllegalArgumentException("Missing 'to'");
        } else if (TextUtils.isEmpty(remoteMessage.getMessageId())) {
            HMSLog.e("HmsMessaging", "Missing 'message_id'");
            a.a(this.c, "push.sendMessage", str, com.huawei.hms.push.a.a.ERROR_ARGUMENTS_INVALID);
            throw new IllegalArgumentException("Missing 'message_id'");
        } else if (TextUtils.isEmpty(remoteMessage.getData())) {
            HMSLog.e("HmsMessaging", "Missing 'data'");
            a.a(this.c, "push.sendMessage", str, com.huawei.hms.push.a.a.ERROR_ARGUMENTS_INVALID);
            throw new IllegalArgumentException("Missing 'data'");
        } else {
            IMessageEntity cVar = new com.huawei.hms.support.api.a.a.c();
            cVar.setPackageName(this.c.getPackageName());
            cVar.setMessageId(remoteMessage.getMessageId());
            cVar.setTo(remoteMessage.getTo());
            cVar.setData(remoteMessage.getData());
            this.d.doWrite(new com.huawei.hms.push.b.a("push.sendMessage", JsonUtil.createJsonString(cVar), str));
        }
    }

    public fjo<Void> turnOnPush() {
        String a = a.a(this.c, "push.setNotifyFlag");
        HMSLog.i("HmsMessaging", "invoke turnOnPush");
        return a(true, a);
    }

    public fjo<Void> turnOffPush() {
        String a = a.a(this.c, "push.setNotifyFlag");
        HMSLog.i("HmsMessaging", "invoke turnOffPush");
        return a(false, a);
    }

    private fjo<Void> a(boolean z, String str) {
        if (!com.huawei.hms.support.api.push.c.a.a(this.c) || com.huawei.hms.support.api.push.c.a.a()) {
            HMSLog.i("HmsMessaging", "turn on/off with AIDL");
            IMessageEntity aVar = new com.huawei.hms.support.api.a.a.a();
            aVar.setPackageName(this.c.getPackageName());
            aVar.setEnable(z);
            return this.d.doWrite(new com.huawei.hms.push.b.a("push.setNotifyFlag", JsonUtil.createJsonString(aVar), str));
        } else if (VERSION.EMUI_SDK_INT < 12) {
            HMSLog.e("HmsMessaging", "operation not available on Huawei device with EMUI lower than 5.1");
            fjx o_fjx = new fjx();
            o_fjx.ॱ(com.huawei.hms.push.a.a.a(com.huawei.hms.push.a.a.ERROR_OPERATION_NOT_SUPPORTED));
            a.a(this.c, "push.setNotifyFlag", str, com.huawei.hms.push.a.a.ERROR_OPERATION_NOT_SUPPORTED);
            return o_fjx.ॱ();
        } else if (com.huawei.hms.support.api.push.c.a.b(this.c) < 90101310) {
            HMSLog.i("HmsMessaging", "turn on/off with broadcast v1");
            Intent putExtra = new Intent("com.huawei.intent.action.SELF_SHOW_FLAG").putExtra("enalbeFlag", com.huawei.hms.support.api.push.c.b.b.a(this.c, this.c.getPackageName() + "#" + z));
            putExtra.setPackage("android");
            return fjw.ˋ(new com.huawei.hms.push.b.b(this.c, putExtra, str));
        } else {
            HMSLog.i("HmsMessaging", "turn on/off with broadcast v2");
            new com.huawei.hms.support.api.push.c.a.a.b(this.c, "push_notify_flag").a("notify_msg_enable", !z);
            Parcelable parse = Uri.parse("content://" + this.c.getPackageName() + ".huawei.push.provider/" + "push_notify_flag" + ".xml");
            Intent intent = new Intent("com.huawei.android.push.intent.SDK_COMMAND");
            intent.putExtra("type", "enalbeFlag");
            intent.putExtra("pkgName", this.c.getPackageName());
            intent.putExtra("url", parse);
            intent.setPackage("android");
            return fjw.ˋ(new com.huawei.hms.push.b.b(this.c, intent, str));
        }
    }
}
